from django.shortcuts import render
from rest_framework.generics import CreateAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from user.serializers import UserCreateSerializer
from .models import *


# Create your views here.
class UsernameCountView(APIView):
    """确认用户是否存在"""

    def get(self, request, username):
        # 查询
        count = User.objects.filter(username=username).count()
        data = {
            'username': username,
            'count': count
        }
        return Response(data)


class MobileCountView(APIView):
    """确认手机号是否合法"""

    def get(self, requst, mobile):
        # 查询
        count = User.objects.filter(mobile=mobile).count()
        data = {
            'mobile': mobile,
            'count': count
        }
        return Response(data)


class CreateUserView(CreateAPIView):
    serializer_class = UserCreateSerializer